package cn.drenal.capcap.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.dto.AdminLoginDTO;
import cn.drenal.capcap.entity.vo.AdminLoginVO;
import cn.drenal.capcap.entity.vo.AdminUserInfoVO;
import cn.drenal.capcap.service.AdminLoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 后台管理登录控制器
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminLoginController {

    private final AdminLoginService adminLoginService;

    /**
     * 会话状态检查接口
     * @return 登录状态及用户信息
     */
    @GetMapping("/check-session")
    public Response<AdminUserInfoVO> checkSession() {
        AdminUserInfoVO vo = new AdminUserInfoVO();
        boolean isLogin = StpUtil.isLogin();
        if (isLogin) {
            long userId = StpUtil.getLoginIdAsLong();
            return Response.success(adminLoginService.getLoginUserInfo(userId));
        }
        vo.setIsLogin(false);
        return Response.success(vo);
    }

    /**
     * 管理员登录
     *
     * @param loginDTO 登录参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public Response<AdminLoginVO> login(@RequestBody AdminLoginDTO loginDTO) {
        return Response.success(adminLoginService.login(loginDTO));
    }

    /**
     * 管理员登出
     *
     * @return 登出结果
     */
    @PostMapping("/logout")
    public Response<Void> logout() {
        adminLoginService.logout();
        return Response.success();
    }

    /**
     * 获取当前登录用户的权限列表
     * @return 权限标识列表
     */
    @GetMapping("/permissions")
    public Response<List<String>> getPermissions() {
        if (!StpUtil.isLogin()) {
            return Response.success(Collections.emptyList());
        }
        long userId = StpUtil.getLoginIdAsLong();
        return Response.success(adminLoginService.getPermissions(userId));
    }
}